/***************************************************************************
 *      mymath.h
 *		
 *      Copyright 2012 Artem Korzhimanov <korzhimanov.artem@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 ***************************************************************************/

#ifndef MYMATH_H_INCLUDED
#define MYMATH_H_INCLUDED

#include <cstring>
#include <cmath>
#include <gsl/gsl_math.h>

class MyMath
{
public:
	MyMath();
	virtual ~MyMath();
	void zeros(double *a, const int number);
	void zeros(double *a, const int number, const int step);
	
	double sum(double *a, const int number, const int step = 1);
	
	template<typename Type> Type max(Type *a, const int number, const int step = 1);
	template<typename Type> Type min(Type *a, const int number, const int step = 1);
	template<typename Type> Type sqr(Type x);
};

inline void MyMath::zeros(double *a, const int number)
{
	memset(a, 0, sizeof(*a)*number);
}

template<typename Type>
Type MyMath::max(Type *a, const int number, const int step)
{
	Type tmp = *a, *t = a+1, *until = a + number*step;
	for (; t < until; t += step) tmp = (tmp < *t) ? *t : tmp;
	return tmp;
}

template<typename Type>
Type MyMath::min(Type *a, const int number, const int step)
{
	Type tmp = *a, *t = a+1, *until = a + number*step;
	for (; t < until; t += step) tmp = (tmp > *t) ? *t : tmp;
	return tmp;
}

template<typename Type>
inline Type MyMath::sqr(Type x)
{
	return x*x;
}

#endif // MYMATH_H_INCLUDED
